<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$traderRole = substr($trader, 0,3);

if($traderRole=='buy'){
	$shout = 'BidId';
	$opponent = 'AskId';
}else if($traderRole=='sel'){
	$shout = 'AskId';
	$opponent = 'BidId';
}

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($traderNo > 0 & $traderNo < 50){
	$trader=$trader.'_'.$traderNo;	
}else{
	$trader=$trader.'%';
}

if($specialist!=''){
	$querySpecialist = " AND s.SpecialistId = '".$specialist."' ";
}else{
	$querySpecialist = "";
}


	$sql="SELECT t.Id AS ID, t.Price AS TransactionPrice, t.".$opponent." AS opponent, s.TraderId AS TraderId, s.Price AS ShoutPrice, s.PrivateValue AS PrivateValue, s.Balance AS Balance
FROM x_shout s, x_trader d, x_transaction t
WHERE s.TraderId LIKE '".$trader."'
AND s.ShoutId = t.".$shout."
AND s.TraderId = d.TraderId ".$querySpecialist." 
AND t.ExperimentId = '".$Experiment."'
AND t.ExperimentId = s.ExperimentId
AND t.ExperimentId = d.ExperimentId
AND s.Status = 'PLACED'
ORDER BY t.Id ASC LIMIT ".$start.",".$items.";";


	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$TransactionId = array();
	$TraderId = array();
	$TransactionPrice = array();
	$ShoutPrice = array();
	$OpponentShoutPrice = array();
	$OpponentId = array();
	$PrivateValue = array();
	$Balance = array();
	$i=0;
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{		
		$TransactionId[$i] = $row['TransactionId'];
		$TraderId[$i] = $row['TraderId'];
		$TransactionPrice[$i] = round($row['TransactionPrice'], 3);
		$ShoutPrice[$i] = round($row['ShoutPrice'], 3 );  
		$PrivateValue[$i] = round($row['PrivateValue'], 3); 
		$Balance[$i] = round(abs($row['Balance']), 3);

		$subSql= "SELECT TraderId, Price FROM x_shout WHERE ShoutId = '".$row['opponent']."' AND ExperimentId = '".$Experiment."'";
		$subResult = mysql_query($subSql,$link) or die ("Can not read db!".mysql_error());
		while($subRow = mysql_fetch_array($subResult, MYSQL_ASSOC)){
			$OpponentShoutPrice[$i] = round($subRow['Price']);
			$OpponentId[$i] = round($subRow['TraderId']);
		}


		$i++;		
	} 


$bar_TransactionPrice = new bar_filled('#43ACF7', '#284BFF');
$bar_TransactionPrice->set_values( $TransactionPrice );
$bar_TransactionPrice->set_tooltip( 'Transaction Price: #val#' );
$bar_TransactionPrice->set_key( "Transaction Price", 10 );

$line_OpponentShoutPrice = new area_line();
$line_OpponentShoutPrice->set_width( 2 );
$line_OpponentShoutPrice->set_colour( '#C82A33' );
$line_OpponentShoutPrice->set_fill_colour( '#F7C9B4' );
$line_OpponentShoutPrice->set_fill_alpha( 0.7 );
$line_OpponentShoutPrice->set_dot_size( 3 );
$line_OpponentShoutPrice->set_values( $OpponentShoutPrice );
$line_OpponentShoutPrice->set_tooltip( 'Opponent Shout Price: #val#' );
$line_OpponentShoutPrice->set_key( "Opponent Shout Price", 10 );

$line_ShoutPrice = new line_hollow();
$line_ShoutPrice->set_width( 1 );
$line_ShoutPrice->set_colour( '#ff0000' );
$line_ShoutPrice->set_dot_size( 3 );
$line_ShoutPrice->set_values( $ShoutPrice );
$line_ShoutPrice->set_tooltip( 'Shout Price: #val#' );
$line_ShoutPrice->set_key( "Shout Price", 10 );

$line_PrivateValue = new line_dot();
$line_PrivateValue->set_width( 1 );
$line_PrivateValue->set_colour( '#5E4725' );
$line_PrivateValue->set_dot_size( 3 );
$line_PrivateValue->set_values( $PrivateValue );
$line_PrivateValue->set_tooltip( 'Private Value: #val#' );
$line_PrivateValue->set_key( "Private Value", 10 );

$line_Balance = new line();
$line_Balance->set_width( 1 );
$line_Balance->set_colour( '#0E4725' );
$line_Balance->set_dot_size( 3 );
$line_Balance->set_values( $Balance );
$line_Balance->set_tooltip( 'Balance: #val#' );
$line_Balance->set_key( "Balance", 10 );

$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.'  -  '.$specialist.'  ('.$trader.' : '.$start.' - '.$final.')' ) );

$x_legend = new x_legend( 'Transaction success' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Price' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );
if($start >= 1000){
	$x_labels = new x_axis_labels();
	$x_labels->set_vertical();
	$x->set_labels( $x_labels );
}

$chart->set_x_axis( $x );

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( 0, 200, 10 ); 

//
// here we add our data sets to the chart:
//
$chart->add_element( $line_OpponentShoutPrice );
$chart->add_element( $bar_TransactionPrice );
$chart->add_element( $line_ShoutPrice );
$chart->add_element( $line_PrivateValue );
$chart->add_element( $line_Balance );

echo $chart->toPrettyString();

?>
